<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Overseaapi extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	function __construct()
	 {
		parent::__construct();
		$this->load->model('function_m','fun');
		$this->pageSize =$this->config->item('pagenum')?$this->config->item('pagenum'):20;
	 }
	//英华德国国际仓储管理 date 5/9
	public function stocklist()
	{
		$where = " AND flag = 0 ";
		$nowpage = $_REQUEST["pageid"];
		if ($nowpage == "")$nowpage = 1;
		$other = $this->input->get_post('other');
		if($other)
		{
			$where .= " AND Barcode like '%".$other."%'";
			$this->tpl->assign("other",$other);
		}
		$stockbegin = $this->input->get_post('stockbegin');
		if($stockbegin)
		{
			$where .= " AND TotalInStock >= $stockbegin ";
			$this->tpl->assign("stockbegin",$stockbegin);
		}
		$stockend = $this->input->get_post('stockend');
		if($stockend)
		{
			$where .= " AND TotalInStock <= $stockend ";
			$this->tpl->assign("stockend",$stockend);
		}
		//序号
		$orderbyxuhao =  $this->input->get('orderbyxuhao');
		if($orderbyxuhao)
		{
			$orderby =" order by id {$orderbyxuhao}";
			if($orderbyxuhao=='ASC')
			{
				$this->tpl->assign("orderbyxuhao",'DESC');
			}else{
				$this->tpl->assign("orderbyxuhao",'ASC');
			}

		}else{
			//$orderby =" order by id ASC";
			$this->tpl->assign("orderbyxuhao",'DESC');
		}
		//商品编号排序
		$orderbysku =  $this->input->get('orderbysku');
		if($orderbysku)
		{
			$orderby =" order by Barcode {$orderbysku}";
			if($orderbysku=='ASC')
			{
				$this->tpl->assign("orderbysku",'DESC');
			}else{
				$this->tpl->assign("orderbysku",'ASC');
			}

		}else{
			$orderby =" order by Barcode ASC";
			$this->tpl->assign("orderbysku",'DESC');
		}
		//商品库存数排序
		$orderbystock =  $this->input->get('orderbystock');
		if($orderbystock)
		{
			$orderby =" order by TotalInStock {$orderbystock}";
			if($orderbystock=='ASC')
			{
				$this->tpl->assign("orderbystock",'DESC');
			}else{
				$this->tpl->assign("orderbystock",'ASC');
			}

		}else{
			//$orderby =" order by Barcode ASC";
			$this->tpl->assign("orderbystock",'ASC');
		}
		$list = $this->fun->getalllist('t_stocks',$where,$nowpage,$if_csv=0,$this->pageSize,$orderby);
		foreach($list['list'] as $key=>$rs)
		{
			//在途数英华德国仓 (D2)
			$rsdb = $this->db->get_one("SELECT A.sku,C.costprice,sum( A.nowbuynum ) AS amount_num,count(*) AS task_num FROM t_transportlist A LEFT JOIN t_transportationtask B ON A.taskid = B.id LEFT JOIN t_overseaproduct C ON A.skuid=C.id WHERE A.taskid>0 AND ( B.status = 2) AND (A.sku like '%".$rs[Barcode]."%' ) AND B.destination ='英华德国仓 (D2)'group by A.`skuid` DESC order by sku ASC");

			$list['list'][$key]['amount_num'] = $rsdb["amount_num"]?$rsdb["amount_num"]:0;
			//复漫系统库存数
			$sql = "SELECT a.stock FROM db_goods_stock a LEFT JOIN db_goods b ON a.goods_id = b.goods_id WHERE b.goods_sku = '{$rs['Barcode']}' AND a.storage_id = 5 ";
			$res = $this->db->query($sql)->row_array();
			$list['list'][$key]['fuman_stock'] = $res["stock"]?$res["stock"]:0;

			//总待发货
			$waitgoods = 0;
			$ress = $this->fun->getdatabasesdetail('db_order_sell a left join db_order_list b on a.order_id = b.order_id '," AND  a.`goods_sn` = '{$rs['Barcode']}' AND b.order_status in (2,3) AND b.shipping_status = 2 "," sum(a.goods_number) as goods_number");
			if($ress){
				$waitgoods = $waitgoods+$ress['goods_number'];
			}
			$list['list'][$key]['waitgoods'] = $waitgoods; //待发货数量

			//30日内销量  订单列表与fba销量之和
			$salecount = 0;
			$time = time()-30*86400;
			$date = date("Y-m-d H:i:s",$time);
			$result = $this->fun->getdatabasesdetail('db_order_sell a left join db_order_list b on a.order_id = b.order_id '," AND b.dates >= '{$date}' AND b.shipping_status = 2 AND b.order_status in(4,5) AND b.order_source in('正常订单','手工创建','拆分订单','合并订单') AND  a.goods_sn = '{$rs['Barcode']}' "," sum(a.goods_number) as goods_number");
			if($result){
				$salecount = $salecount+$result['goods_number'];
			}
			$list['list'][$key]['salecount'] = $salecount; //30天内的销量


		}
		$pageList = $this->pagenav->page($list["total"], $nowpage, $this->pageSize);
		$this->tpl->assign("totalpage", ceil($list["total"] / $this->pageSize));
		$this->tpl->assign("pageList", $pageList);
		$this->tpl->assign("total", $list["total"]);
		$this->tpl->assign("list",$list["list"]);
		$this ->tpl->display("oversea/yhstocklist.tpl");
	}
	//英华英国仓储管理 date 5/9
	public function ukstocklist()
	{
		//$this->doCrontabUkTask();121
		$where = " AND flag =1";
		$nowpage = $_REQUEST["pageid"];
		if ($nowpage == "")$nowpage = 1;
		$other = $this->input->get_post('other');
		if($other)
		{
			//$where .= " AND Barcode = '".$other."'";
			$where .= " AND Barcode like '%".$other."%'";
			$this->tpl->assign("other",$other);
		}
		$stockbegin = $this->input->get_post('stockbegin');
		if($stockbegin)
		{
			$where .= " AND TotalInStock >= $stockbegin ";
			$this->tpl->assign("stockbegin",$stockbegin);
		}
		$stockend = $this->input->get_post('stockend');
		if($stockend)
		{
			$where .= " AND TotalInStock <= $stockend ";
			$this->tpl->assign("stockend",$stockend);
		}
		//序号
		$orderbyxuhao =  $this->input->get('orderbyxuhao');
		if($orderbyxuhao)
		{
			$orderby =" order by id {$orderbyxuhao}";
			if($orderbyxuhao=='ASC')
			{
				$this->tpl->assign("orderbyxuhao",'DESC');
			}else{
				$this->tpl->assign("orderbyxuhao",'ASC');
			}

		}else{
			//$orderby =" order by id ASC";
			$this->tpl->assign("orderbyxuhao",'DESC');
		}
		//商品编号排序
		$orderbysku =  $this->input->get('orderbysku');
		if($orderbysku)
		{
			$orderby =" order by Barcode {$orderbysku}";
			if($orderbysku=='ASC')
			{
				$this->tpl->assign("orderbysku",'DESC');
			}else{
				$this->tpl->assign("orderbysku",'ASC');
			}

		}else{
			$orderby =" order by Barcode ASC";
			$this->tpl->assign("orderbysku",'DESC');
		}
		//商品库存数排序
		$orderbystock =  $this->input->get('orderbystock');
		if($orderbystock)
		{
			$orderby =" order by TotalInStock {$orderbystock}";
			if($orderbystock=='ASC')
			{
				$this->tpl->assign("orderbystock",'DESC');
			}else{
				$this->tpl->assign("orderbystock",'ASC');
			}

		}else{
			//$orderby =" order by Barcode ASC";
			$this->tpl->assign("orderbystock",'ASC');
		}
		$list = $this->fun->getalllist('t_stocks',$where,$nowpage,$if_csv=0,$this->pageSize,$orderby);
		foreach($list['list'] as $key=>$rs)
		{
			//在途数英华英国仓
			$rsdb = $this->db->get_one("SELECT A.sku,C.costprice,sum( A.nowbuynum ) AS amount_num,count(*) AS task_num FROM t_transportlist A LEFT JOIN t_transportationtask B ON A.taskid = B.id LEFT JOIN t_overseaproduct C ON A.skuid=C.id WHERE A.taskid>0 AND ( B.status = 2) AND (A.sku like '%".$rs[Barcode]."%' ) AND B.destination ='英华英国仓 (E2)'group by A.`skuid` DESC order by sku ASC");

			$list['list'][$key]['amount_num'] = $rsdb["amount_num"]?$rsdb["amount_num"]:0;
			//复漫系统库存数
			$sql = "SELECT a.stock FROM db_goods_stock a LEFT JOIN db_goods b ON a.goods_id = b.goods_id WHERE b.goods_sku = '{$rs['Barcode']}' AND a.storage_id = 3 ";
			$res = $this->db->query($sql)->row_array();
			$list['list'][$key]['fuman_stock'] = $res["stock"]?$res["stock"]:0;

			//总待发货
			$waitgoods = 0;
			$ress = $this->fun->getdatabasesdetail('db_order_sell a left join db_order_list b on a.order_id = b.order_id '," AND  a.`goods_sn` = '{$rs['Barcode']}' AND b.order_status in (2,3) AND  b.shipping_status = 1 "," sum(a.goods_number) as goods_number");
			if($ress){
				$waitgoods = $waitgoods+$ress['goods_number'];
			}
			$list['list'][$key]['waitgoods'] = $waitgoods; //待发货数量

			//30日内销量  订单列表与fba销量之和
			$salecount = 0;
			$time = time()-30*86400;
			$date = date("Y-m-d H:i:s",$time);
			$result = $this->fun->getdatabasesdetail('db_order_sell a left join db_order_list b on a.order_id = b.order_id '," AND b.dates >= '{$date}' AND b.shipping_status = 1 AND b.order_status in(4,5) AND b.order_source in('正常订单','手工创建','拆分订单','合并订单') AND  a.goods_sn = '{$rs['Barcode']}' "," sum(a.goods_number) as goods_number");
			if($result){
				$salecount = $salecount+$result['goods_number'];
			}
			$list['list'][$key]['salecount'] = $salecount; //30天内的销量
		}
		$pageList = $this->pagenav->page($list["total"], $nowpage, $this->pageSize);
		$this->tpl->assign("totalpage", ceil($list["total"] / $this->pageSize));
		$this->tpl->assign("pageList", $pageList);
		$this->tpl->assign("total", $list["total"]);
		$this->tpl->assign("list",$list["list"]);
		$this ->tpl->display("oversea/ukstocklist.tpl");
	}
	//本地仓库EZGO中相关存在数据 2018/1/22
	public function usastocklist()
	{

		$where = " AND a.storage_id = 6";
		$nowpage = $_REQUEST["pageid"];
		if ($nowpage == "")$nowpage = 1;
		$other = $this->input->get_post('other');
		if($other)
		{
			$where .= " AND b.goods_sku like '%".$other."%'";
			$this->tpl->assign("other",$other);
		}
		$stockbegin = $this->input->get_post('stockbegin');
		if($stockbegin)
		{
			$where .= " AND a.stock >= $stockbegin ";
			$this->tpl->assign("stockbegin",$stockbegin);
		}
		$stockend = $this->input->get_post('stockend');
		if($stockend)
		{
			$where .= " AND a.stock <= $stockend ";
			$this->tpl->assign("stockend",$stockend);
		}
		//序号
		$orderbyxuhao =  $this->input->get('orderbyxuhao');
		if($orderbyxuhao)
		{
			$orderby =" order by a.id {$orderbyxuhao}";
			if($orderbyxuhao=='ASC')
			{
				$this->tpl->assign("orderbyxuhao",'DESC');
			}else{
				$this->tpl->assign("orderbyxuhao",'ASC');
			}

		}else{
			//$orderby =" order by id ASC";
			$this->tpl->assign("orderbyxuhao",'DESC');
		}
		//商品编号排序
		$orderbysku =  $this->input->get('orderbysku');
		if($orderbysku)
		{
			$orderby =" order by b.goods_sku {$orderbysku}";
			if($orderbysku=='ASC')
			{
				$this->tpl->assign("orderbysku",'DESC');
			}else{
				$this->tpl->assign("orderbysku",'ASC');
			}

		}else{
			$orderby =" order by b.goods_sku ASC";
			$this->tpl->assign("orderbysku",'ASC');
		}
		//商品库存数排序
		$orderbystock =  $this->input->get('orderbystock');
		if($orderbystock)
		{
			$orderby =" order by a.stock {$orderbystock}";
			if($orderbystock=='ASC')
			{
				$this->tpl->assign("orderbystock",'DESC');
			}else{
				$this->tpl->assign("orderbystock",'ASC');
			}

		}else{
			//$orderby =" order by Barcode ASC";
			$this->tpl->assign("orderbystock",'DESC');
		}
		$join = " LEFT JOIN db_goods b ON a.goods_id = b.goods_id ";
		$list = $this->fun->getalllist('db_goods_stock a',$where,$nowpage,$if_csv=0,$this->pageSize,$orderby,$fields='*',$join);
		foreach($list['list'] as $key=>$rs){
			//总待发货
			$waitgoods = 0;
			$ress = $this->fun->getdatabasesdetail('db_order_sell a left join db_order_list b on a.order_id = b.order_id '," AND  a.`goods_id` = '{$rs['goods_id']}' AND b.order_status in (2,3) AND b.shipping_status = 3 "," sum(a.goods_number) as goods_number");
			if($ress){
				$waitgoods = $waitgoods+$ress['goods_number'];
			}
			$list['list'][$key]['waitgoods'] = $waitgoods; //待发货数量

			//30日内销量  订单列表与fba销量之和
			$salecount = 0;
			$time = time()-30*86400;
			$date = date("Y-m-d H:i:s",$time);
			$result = $this->fun->getdatabasesdetail('db_order_sell a left join db_order_list b on a.order_id = b.order_id '," AND b.dates >= '{$date}' AND b.shipping_status = 3 AND b.order_status in(4,5) AND b.order_source in('正常订单','手工创建','拆分订单','合并订单') AND  a.goods_id = '{$rs['goods_id']}' "," sum(a.goods_number) as goods_number");
			if($result){
				$salecount = $salecount+$result['goods_number'];
			}
			$list['list'][$key]['salecount'] = $salecount; //30天内的销量

		}

		$pageList = $this->pagenav->page($list["total"], $nowpage, $this->pageSize);
		$this->tpl->assign("totalpage", ceil($list["total"] / $this->pageSize));
		$this->tpl->assign("pageList", $pageList);
		$this->tpl->assign("total", $list["total"]);
		$this->tpl->assign("list",$list["list"]);
		$this ->tpl->display("oversea/usastocklist.tpl");
	}
	//海外美国仓储管理 date 5/9     该海外仓不用了 2018/1/22
	public function usastocklist_stop()
	{
		//$ceshi = $this->doCrontabUsaTask();
		//var_dump($ceshi);exit;
		$where = " AND flag = 2";
		$nowpage = $_REQUEST["pageid"];
		if ($nowpage == "")$nowpage = 1;
		$other = $this->input->get_post('other');
		if($other)
		{
			//$where .= " AND Barcode = '".$other."'";
			$where .= " AND Barcode like '%".$other."%'";
			$this->tpl->assign("other",$other);
		}
		$stockbegin = $this->input->get_post('stockbegin');
		if($stockbegin)
		{
			$where .= " AND TotalInStock >= $stockbegin ";
			$this->tpl->assign("stockbegin",$stockbegin);
		}
		$stockend = $this->input->get_post('stockend');
		if($stockend)
		{
			$where .= " AND TotalInStock <= $stockend ";
			$this->tpl->assign("stockend",$stockend);
		}
		//序号
		$orderbyxuhao =  $this->input->get('orderbyxuhao');
		if($orderbyxuhao)
		{
			$orderby =" order by id {$orderbyxuhao}";
			if($orderbyxuhao=='ASC')
			{
				$this->tpl->assign("orderbyxuhao",'DESC');
			}else{
				$this->tpl->assign("orderbyxuhao",'ASC');
			}

		}else{
			//$orderby =" order by id ASC";
			$this->tpl->assign("orderbyxuhao",'DESC');
		}
		//商品编号排序
		$orderbysku =  $this->input->get('orderbysku');
		if($orderbysku)
		{
			$orderby =" order by Barcode {$orderbysku}";
			if($orderbysku=='ASC')
			{
				$this->tpl->assign("orderbysku",'DESC');
			}else{
				$this->tpl->assign("orderbysku",'ASC');
			}

		}else{
			$orderby =" order by Barcode ASC";
			$this->tpl->assign("orderbysku",'DESC');
		}
		//商品库存数排序
		$orderbystock =  $this->input->get('orderbystock');
		if($orderbystock)
		{
			$orderby =" order by TotalInStock {$orderbystock}";
			if($orderbystock=='ASC')
			{
				$this->tpl->assign("orderbystock",'DESC');
			}else{
				$this->tpl->assign("orderbystock",'ASC');
			}

		}else{
			//$orderby =" order by Barcode ASC";
			$this->tpl->assign("orderbystock",'DESC');
		}
		$list = $this->fun->getalllist('t_stocks',$where,$nowpage,$if_csv=0,$this->pageSize,$orderby);
		foreach($list['list'] as $key=>$rs)
		{
			//在途数EZGO美国仓
			$rsdb = $this->db->get_one("SELECT A.sku,C.costprice,sum( A.nowbuynum ) AS amount_num,count(*) AS task_num FROM t_transportlist A LEFT JOIN t_transportationtask B ON A.taskid = B.id LEFT JOIN t_overseaproduct C ON A.skuid=C.id WHERE A.taskid>0 AND ( B.status = 2) AND (A.sku like '%".$rs[Barcode]."%' ) AND B.destination ='EZGO美国仓 (U2)'group by A.`skuid` DESC order by sku ASC");
			$list['list'][$key]['amount_num'] = $rsdb["amount_num"]?$rsdb["amount_num"]:0;
			//复漫系统库存数
			$sql = "SELECT a.stock FROM db_goods_stock a LEFT JOIN db_goods b ON a.goods_id = b.goods_id WHERE b.goods_sku = '{$rs['Barcode']}' AND a.storage_id = 6 ";
			$res = $this->db->query($sql)->row_array();
			$list['list'][$key]['fuman_stock'] = $res["stock"]?$res["stock"]:0;

		}
		$pageList = $this->pagenav->page($list["total"], $nowpage, $this->pageSize);
		$this->tpl->assign("totalpage", ceil($list["total"] / $this->pageSize));
		$this->tpl->assign("pageList", $pageList);
		$this->tpl->assign("total", $list["total"]);
		$this->tpl->assign("list",$list["list"]);
		$this ->tpl->display("oversea/usastocklist.tpl");
	}
	//海外美国仓   该海外仓不用了 2018/1/22
	public function doCrontabUsaTask(){
		ini_set('max_execution_time',0);
		//判断该请求是否有执行该函数的权限
		$value = $this->input->get('params');
		if($value&&$value=='e29e0068cb5778e7426492247173151d'){
		}else{
			echo json_encode(array('code'=>0,'msg'=>'参数错误!'));exit;
		}
		$customerId = $this->config->item('goodscode');
		$token = $this->config->item('usatoken');
		$usasuppliercode = $this->config->item('usasuppliercode');
		//openapi.4pis.com 正式
		//$url = "http://openapi.4pis.com:8080/api/service/wmc/basic/searchStockWarehouseQty?token=$token&customerId=$customerId&language=zh_CN";
		$url = "http://openapi.4pis.com:8080/api/service/wmc/basic/searchStockWarehouseQty?token=$token&customerId=$customerId&language=zh_CN";
		//http://openapi.4pis.com:8080/api/service/wmc/basic/searchStockWarehouseQty?token=23420E0534DFEB3FBEAF8AB135FCC321&customerId=802401&language=zh_CN
		//测试
		//$url = "http://apisandbox.4pis.com/api/service/wmc/basic/searchStockWarehouseQty";
		$headers= array("Content-type: Application/json;charset=utf-8");
		$post_data = array('warehouseId'=>"$usasuppliercode",);
		$post_data = json_encode($post_data);
		$post_arr_result = $this->sendCurlPostRequest($url,$post_data,$headers);
		//获取库存数量
			if(count($post_arr_result['data'])>0){
				//获取对应的名称
				$arr_stocks = array();
				foreach($post_arr_result['data'] as $k=>$v){
					$arr_stocks['Barcode']=$v['productId'];
					$arr_stocks['TotalInStock']=$v['stockAvailableQty'];
					//执行数据库更新或插入操作
					$sql_sel= "SELECT * FROM t_stocks WHERE flag = 2 AND Barcode = '{$v['productId']}' ";
					$result =$this->db->query($sql_sel)->row_array();
					$addtime = date("Y-m-d H:i:s");
					$arr_stocks['addtime']=$addtime;
					if(count($result)>0){
						$arr_upd['TotalInStock'] =$v['stockAvailableQty'];
						$arr_upd['addtime'] =$addtime;
						$this->db->where('Barcode',$v['productId']);
						$this->db->where('flag',2);
						$this->db->update('t_stocks',$arr_upd);
					}else{
						//数据库插入操作
						$arr_stocks['Name']=$v['productName'];
						$arr_stocks['flag']= 2;
						$this->db->insert('t_stocks',$arr_stocks);
					}
				}

			}

	}
	//英华德国仓
	public function doCrontabTask(){
		ini_set('max_execution_time',0);
		//判断该请求是否有执行该函数的权限
		$value = $this->input->get('params');
		if($value&&$value=='e29e0068cb5778e7426492247173151d'){
		}else{
			echo json_encode(array('code'=>0,'msg'=>'参数错误!'));exit;
		}
		$url = "https://yinghuaglobal.de/Token";
		//echo $this->config->item('ukusername');
		//$username = "fuman";
		//$password = "Fm123456";
		$username = $this->config->item('germanyusername');
		$password = $this->config->item('germanypass');
		//获取token
		$post_data = array ("username" => $username,"password" => $password,'grant_type'=>'password');
		$post_data = http_build_query($post_data);
		$headers = array("Content-type: Application/x-www-form-urlencoded;charset=utf-8");
		$post_arr_result = $this->sendCurlPostRequest($url,$post_data,$headers);
		//result['access_token']
		$token = $post_arr_result['access_token'];
		//获取库存数量
		$count = 100;
		for($page =1;$page<$count;$page++){
			$stocks_url = "https://yinghuaglobal.de/wms/Stocks?page=$page&pageSize=50";
			//$stocks_url = "https://yinghuaglobal.de/wms/Stocks?page=$page&pageSize=50";
			$get_arr_result = $this->sendCurlGetRequest($stocks_url,$token);
			//var_dump($get_arr_result);exit;
			if(count($get_arr_result)>0){
				//获取对应的名称
				$arr_stocks = array();
				foreach($get_arr_result as $k=>$v){
					//$arr_stocks['AuditTime']=$v['AuditTime'];
					$arr_stocks['Barcode']=$v['Barcode'];
					$arr_stocks['TotalInStock']=$v['TotalInStock'];
					//$arr_stocks['DistributedQuantity']=$v['DistributedQuantity'];
					//执行数据库更新或插入操作
					$sql_sel= "SELECT * FROM t_stocks WHERE flag = 0 AND Barcode = '{$v['Barcode']}' ";
					$result =$this->db->query($sql_sel)->row_array();
					$addtime = date("Y-m-d H:i:s");
					$arr_stocks['addtime']=$addtime;
					if(count($result)>0){
						$arr_upd['TotalInStock'] =$v['TotalInStock'];
						$arr_upd['addtime'] =$addtime;
						$this->db->where('Barcode',$v['Barcode']);
						$this->db->where('flag',0);
						$this->db->update('t_stocks',$arr_upd);
					}else{
						//获取名字
						$path = "https://yinghuaglobal.de//wms/Products/{$v['Barcode']}";
						$arr_row = $this->sendCurlGetRequest($path,$token);
						$arr_stocks['Name']=$arr_row['Name'];
						$this->db->insert('t_stocks',$arr_stocks);
					}

				}

			}

		} //for循环结束

	}
	//英华英国仓
	public function doCrontabUkTask(){
		ini_set('max_execution_time',0);
		//判断该请求是否有执行该函数的权限
		$value = $this->input->get('params');
		if($value&&$value=='e29e0068cb5778e7426492247173151d'){
		}else{
			echo json_encode(array('code'=>0,'msg'=>'参数错误!'));exit;
		}
		$url = "https://yinghua.co.uk/Token";
		//$username = "fuman";
		//$password = "Cosplaysky2015";
		$username = $this->config->item('ukusername');
		$password = $this->config->item('ukpass');
		//获取token
		$post_data = array ("username" => $username,"password" => $password,'grant_type'=>'password');
		$post_data = http_build_query($post_data);
		$headers = array("Content-type: Application/x-www-form-urlencoded;charset=utf-8");
		$post_arr_result = $this->sendCurlPostRequest($url,$post_data,$headers);
		//result['access_token']
		$token = $post_arr_result['access_token'];
		//获取库存数量
		$count = 100;
		for($page =1;$page<$count;$page++){
			$stocks_url = "https://yinghua.co.uk/wms/Stocks?page=$page&pageSize=50";
			$get_arr_result = $this->sendCurlGetRequest($stocks_url,$token);
			if(count($get_arr_result)>0){
				//获取对应的名称
				$arr_stocks = array();
				foreach($get_arr_result as $k=>$v){
					//$arr_stocks['AuditTime']=$v['AuditTime'];
					$arr_stocks['Barcode']=$v['Barcode'];
					$arr_stocks['TotalInStock']=$v['TotalInStock'];
					//$arr_stocks['DistributedQuantity']=$v['DistributedQuantity'];
					//执行数据库更新或插入操作
					$sql_sel= "SELECT * FROM t_stocks WHERE flag = 1 AND Barcode = '{$v['Barcode']}' ";
					$result =$this->db->query($sql_sel)->row_array();
					$addtime = date("Y-m-d H:i:s");
					$arr_stocks['addtime']=$addtime;
					if(count($result)>0){
						$arr_upd['TotalInStock'] =$v['TotalInStock'];
						$arr_upd['addtime'] =$addtime;
						$this->db->where('Barcode',$v['Barcode']);
						$this->db->where('flag',1);
						$this->db->update('t_stocks',$arr_upd);
					}else{
						//获取名字
						$path = "https://yinghua.co.uk//wms/Products/{$v['Barcode']}";
						$arr_row = $this->sendCurlGetRequest($path,$token);
						$arr_stocks['Name']=$arr_row['Name'];
						$arr_stocks['flag']=1;
						$this->db->insert('t_stocks',$arr_stocks);
					}

				}

			}

		} //for循环结束

	}
	//海外仓发货列表
	public function showSendGoodsListOversea(){
		$t1 = microtime(true);
		$nowpage = $_REQUEST["pageid"];
		if ($nowpage == "") $nowpage = 1;

		$where = " AND a.order_status !=7";

		$join = "";
		//new add  2017/7/18
		$where.=" AND (a.shipping_status = 1 or a.shipping_status = 2 or a.shipping_status = 3 or a.shipping_status = 4 ) ";

		if($this->input->get_post('order_id'))
		{
			$order_id = $this->input->get_post('order_id');
			$where.=" AND a.order_id in($order_id)";
		}

		if ($this->input->get_post('other')) {
			$other = trim($this->input->get_post('other'));
			if (strstr($other, ",")) {
				$others = explode(",", $other);
				$where .= " and (";
				foreach ($others as $key => $r) {
					if ($key > 0) $k = "or";
					$where .= " $k a.amzorderid='" . $r . "'";
				}
				$where .= ")";

			} else {
				$where .= " and  (
					 a.order_sn =   '" . $other . "'
					) ";
			}
			$this->tpl->assign("other", trim($this->input->get_post('other')));
		}
		if (intval($this->input->get_post('ordersearchID'))) {
			$ordersearchID = intval($this->input->get_post('ordersearchID'));
			$ordersearch = $this->m->getordersearch($ordersearchID);
			if ($ordersearch[0]["name"]) {
				if ($ordersearch[0]["sqlfilter"])
					$where .= $ordersearch[0]["sqlfilter"];
				$this->tpl->assign("ordersearchname", $ordersearch[0]["name"]);
				//$this->tpl->assign("ordersearchID", $ordersearchID);
			}

		}
		if ($this->input->get('ty') == 1 || $this->input->get('ty') == '') $tys = 'a.ordertime';
		if ($this->input->get('ty') == 2) $tys = 'a.add_time';
		if ($this->input->get('ty') == 3) $tys = 'a.shipping_time';
		if ($this->input->get('ty') == 4) $tys = 'a.finshtime';
		$this->tpl->assign("ty", trim($this->input->get_post('ty')));
		$starttime = trim($this->input->get('starttime'));
		if ($starttime) {
			$times = $starttime . " 00:00:00";
			if ($this->input->get('ty') == 2 || $this->input->get('ty') == 3) $times = strtotime($times);
			$where .= " AND ($tys >= '" . $times . "')";
			$this->tpl->assign("starttime", $starttime);
		}

		$endtime = trim($this->input->get('endtime'));
		if ($endtime) {
			$times = $endtime . " 23:59:59";
			if ($this->input->get('ty') == 2 || $this->input->get('ty') == 3) $times = strtotime($times);
			$where .= " AND ($tys <= '" . $times . "')";
			$this->tpl->assign("endtime", $endtime);
		}
		if (intval($this->input->get_post('current')))    //今日订单
		{
			$this->tpl->assign("current", 1);
		}
		if (intval($this->input->get_post('isow')))    //超重
		{

			$where .= " and  (a.is_overweight = 1) ";
		}
		if (intval($this->input->get_post('isno')))    //禁止发货
		{
			$where .= " and  (a.is_nodelivery = 1) ";
		}
		if (intval($this->input->get_post('que')))    //是否缺货
		{
			$que = intval($this->input->get_post('que'));
			$where .= " and  (a.is_que = '" . $que . "') ";
		}
		//平台

		if (intval($this->input->get_post('platformID')))    //是否缺货
		{
			$platformID = intval($this->input->get_post('platformID'));
			$this->tpl->assign("platformID", $platformID);
			if($platformID && intval($this->input->get_post('shopID'))=='')
			{
				$shopIDList = $this->m->getshopIDlist($platformID);
				if($shopIDList)
				{
					$where .= " and  (a.shopid in ($shopIDList)) ";
				}
			}
		}
		if (intval($this->input->get_post('shopID'))) {
			$shopID = intval($this->input->get_post('shopID'));
			$where .= " and  (a.shopid =   '" . $shopID . "') ";
			$this->tpl->assign("shopID", $shopID);
		}
		if (intval($this->input->get_post('status'))) {
			$status = intval($this->input->get_post('status'));
			$flag = $this->input->get_post('flag');
			if($status==6&&$flag==1){
				$where.=" AND b.del_flag != 1 ";
				$join = " LEFT JOIN db_order_oversea b ON a.order_sn = b.order_sn ";
			}elseif($status==6){
				$join = " LEFT JOIN db_order_oversea b ON a.order_sn = b.order_sn ";
			}
			else{

			}
			$where .= " and  (a.order_status =   '" . $status . "') ";
			$this->tpl->assign("status", $status);
		}else{
			$where.=" AND a.order_status !=6";
		}
		if (intval($this->input->get_post('expresstype'))) {
			$expresstype = intval($this->input->get_post('expresstype'));
			$where .= " and  (a.shipping_id =   '" . $expresstype . "') ";
			$this->tpl->assign("expresstypeID", $expresstype);
		}

		if ($this->input->get_post('filtertype')) {
			$filtertype = trim($this->input->get_post('filtertype'));
			$kw = trim($this->input->get_post('kw'));
			if ($filtertype == 'productname') {
				$where .= " and a.order_id in(select distinct order_id from db_order_sell where goods_name like '" . $kw . "')";
			} elseif ($filtertype == 'productid') {
				$where .= " and a.order_id in(select distinct order_id from db_order_sell where goods_sn like '" . $kw . "')";
			} elseif ($filtertype == 'custome') {
				$where .= " and  (a.customerid like '%" . $kw . "%') ";
			} else {
				$where .= " and  ($filtertype =   '" . $kw . "') ";
			}
			$this->tpl->assign("filtertype", $filtertype);
			$this->tpl->assign("kw", $kw);
		}
		if ($this->input->get_post('filtertype') == "" && trim($this->input->get_post('kw'))) {
			$kw = trim($this->input->get_post('kw'));
			$where .= " and  (
					 a.order_sn like   '%" . $kw . "%'
					 or a.tradeid like '%" . $kw . "%'
					) ";
			$this->tpl->assign("kw", $kw);
		}

		if ($this->input->get_post('st')) {
			$st = $this->input->get_post('st');
			$where .= " and  (a.order_source =   '" . $st . "') ";
			$this->tpl->assign("st", $st);
		}

		/*查询绑定的店铺订单**/
		if($_COOKIE["department"] ==1 || $_COOKIE["department"] ==2 || $_COOKIE["classid"] ==10)
		{

		}else{
			$shopname = $_COOKIE["shopname"];	//开通了权限的人员需要绑定店铺
			$shopname = unserialize($shopname);
			if($shopname)
			{
				$shopwhere = "";
				if(in_array('all',$shopname))	//可以查看所有店铺的订单
				{

				}else{
					$where.= " AND (";
					$shopwhere.=" AND (";
					foreach($shopname as $key=>$name)
					{
						if($key>0)
						{
							$or = " or";
						}
						$where.=" $or a.shopname = '".$name."'";
						$shopwhere.=" $or a.name = '".$name."'";
					}
					$where.=") ";
					$shopwhere.=") ";
				}

			}else{
				$where.= " AND a.shopname ='999'";//查询一个不存在的店铺
			}
		}
		//print_r($this->input->get());
		$orderby = ' order by a.order_status ASC ,a.order_id DESC';
		if($join){
			$fileds  = "a.*,b.del_flag";
		}else{
			$fileds = "a.*";
		}
		$list = $this->fun->getalllist("db_order_list a $dbname", $where, $nowpage, $if_csv, $this->pageSize, $orderby, $fileds,$join);
		//echo $this->db->last_query();exit;
		foreach ($list["list"] as $key => $rs) {

			switch ($rs["order_status"]) {
				case 1;
					$list["list"][$key]["statustext"] = '新订单';
					break;
				case 2:
					$list["list"][$key]["statustext"] = '已支付';
					break;
				case 3:
					$list["list"][$key]["statustext"] = '配货中';
					break;
				case 4:
					$list["list"][$key]["statustext"] = '已发货';
					break;
				case 5:
					$list["list"][$key]["statustext"] = '已完成';
					break;
				case 6:
					$list["list"][$key]["statustext"] = '已作废';
					break;
				case 7:
					$list["list"][$key]["statustext"] = '待审查';
					break;
			}

			$list["list"][$key]["numday"] = intval((time() - strtotime($rs["ordertime"])) / 86400); //延迟天数
			$list["list"][$key]["goods_total"] = $rs["goods_amount"] + $rs["shipping_fee"]; //订单合计金额 = 商品金额+运费-平台-支付-商品成本

			$shop = $this->fun->getdatabasesdetail("d_shop", " AND name='" . $rs["shopname"] . "'");    //负责人
			$list["list"][$key]["responsible"] = $shop["updateoper"];
			$list["list"][$key]["platfromname"] = $shop["platfromname"];
			$list["list"][$key]["add_time"] = date("Y-m-d H:i:s",$rs["add_time"]);
		}
		$pageList = $this->pagenav->page($list["total"], $nowpage, $this->pageSize);
		$this->tpl->assign("totalpage", ceil($list["total"] / $this->pageSize));
		$this->tpl->assign("total", $list["total"]);
		$this->tpl->assign("pageList", $pageList);
		if($platformID){
			$whereshop = " AND platfrom = ".$platformID."";
		}
		if($shopwhere)
		{
			$whereshop.=$shopwhere;
		}
		$this->tpl->assign("d_shop", $this->fun->getdatabaseslist('d_shop', " AND del_flag= 0 $whereshop", 'id,name', ' order by name asc'));            //查所有店铺
		$this->tpl->assign("orderlist", $list["list"]);
		$this->tpl->assign("today", date("Y-m-d"));
		$this->tpl->assign("platform", $this->fun->getdatabaseslist('db_platform'));//所有平台
		//$this->tpl->assign("expresstype", $this->m->getorderexpress());//货运方式
		//$this->tpl->assign("ordersearch", $this->m->getordersearch());//订单搜索脚本

		$t2 = microtime(true);
		$memory_get_usage = round(memory_get_usage()/1024,2);
		$vals='页面执行耗时 '.round($t2-$t1,3).'秒&nbsp;&nbsp;&nbsp;';
		$this->tpl->assign("showmemorygetusage", $vals);
		$this->tpl->display("oversea/sendgoodslist.tpl");

	}
	public function ajaxbutton()
	{
		$op = $this->input->get_post('op');
		if($op=="deposeOrderOversea"){
			$order_id = trim($this->input->post('order_id'));
			$order_id = trim($order_id,",");
			$arr_order_id = explode(",", $order_id);
			$str_conn = "";
			foreach($arr_order_id as $k=>$v){
				$str_conn.= "'".$v."'".",";
			}
			$str_conn = trim($str_conn,",");
			//print_r($str_conn);exit;
			$sql = "UPDATE db_order_oversea SET del_flag = 1 WHERE order_sn in ($str_conn) ";
			$rs  = $this->db->query($sql);
			if($this->db->affected_rows()){
				$arr_data = array('code'=>200,"txt"=>"更新成功");
			}else{
				$arr_data = array('code'=>201,"txt"=>"更新失败");
			}
			echo json_encode($arr_data);exit;


		}
		//海外仓无法撤单向相关人员发送消息
		if($op=="sendMessaggeOversea"){
			$order_id = trim($this->input->post('order_id')); //实际为订单编号 order_sn
			$order_id = trim($order_id,",");
			$arr_order_id = explode(",", $order_id);
			//设置系统Message提醒
			if($this->config->item('overseatipopen')==1)
			{
				foreach($arr_order_id as $key=>$value){

						$sql = "SELECT * FROM db_order_oversea WHERE order_sn = '".$value."'";
						$result  = $this->db->query($sql)->row_array();
						//$packingman[] = $_COOKIE['username']; //测试
						$packingman[] = $result['username'];
						$randid = $this->message->getMessageNums();	//获取当前存储的条数
						//$packingman = explode("|",$this->config->item('overseareceiver'));
						foreach($packingman as $rs)
						{
							$postMessageData["message_type"] = $this->message->getMessageType(8);	//海外仓撤单
							$message = $_COOKIE["username"]."[".date("Y-m-d H:i:s")."]"."作废订单".$value;
							$message.="，海外仓撤单失败！";
							$postMessageData["message"] = $message;
							$postMessageData["message_toName"] = $rs;
							$postMessageData["isread"] =  $randid;
							$postMessageData["url"] = '/orderlist/orderlist?kw='.$value;
							$this->fun->saveMessage($postMessageData);
							$this->db->update('db_order_oversea',array('del_flag'=>3),"order_sn = ".$value);
						}

				}
			}
			$arr_data = array('code'=>200,"txt"=>"发送成功");
			echo json_encode($arr_data);exit;


		}

	}
	//英华德国国际仓储管理 date 5/9
	public function showOrderLogOversea()
	{
		$where = "";
		$where.=" AND a.del_flag = 0 ";
		$nowpage = $_REQUEST["pageid"];
		if ($nowpage == "")$nowpage = 1;
		$order_sn = $this->input->get_post('order_sn');
		if($order_sn)
		{
			$where .= " AND a.order_sn = '{$order_sn}'";
			$this->tpl->assign("order_sn",$order_sn);
		}
		$status = $this->input->get_post('status');
		if($status==1)
		{
			$where .= " AND a.isSuccess = $status ";
			$this->tpl->assign("status",$status);
		}
		if($status==="0")
		{
			$where .= " AND a.isSuccess = 0 ";
			$this->tpl->assign("status",$status);
		}
		$orderby = " order by a.id desc";
		$fields = "a.id,a.order_sn,a.salesOrderId,a.errorMessage,a.isSuccess,a.addTime,b.shipping_number,b.shipping_status ";
		$join = "LEFT JOIN db_order_list b ON a.order_id = b.order_id ";
		$list = $this->fun->getalllist('db_order_oversea a',$where,$nowpage,$if_csv=0,$this->pageSize,$orderby,$fields,$join);
		$pageList = $this->pagenav->page($list["total"], $nowpage, $this->pageSize);
		$this->tpl->assign("totalpage", ceil($list["total"] / $this->pageSize));
		$this->tpl->assign("pageList", $pageList);
		$this->tpl->assign("total", $list["total"]);
		$this->tpl->assign("list",$list["list"]);
		$this ->tpl->display("oversea/orderlistlog.tpl");
	}
	//万邑联 Winit 美国 仓储管理
	public function winitstocklist()
	{
		//$ceshi = $this->doCrontabUsaTask();
		//var_dump($ceshi);exit;
		$where = " AND flag = 3";
		$nowpage = $_REQUEST["pageid"];
		if ($nowpage == "")$nowpage = 1;
		$other = $this->input->get_post('other');
		if($other)
		{
			//$where .= " AND Barcode = '".$other."'";
			$where .= " AND Barcode like '%".$other."%'";
			$this->tpl->assign("other",$other);
		}
		$stockbegin = $this->input->get_post('stockbegin');
		if($stockbegin)
		{
			$where .= " AND TotalInStock >= $stockbegin ";
			$this->tpl->assign("stockbegin",$stockbegin);
		}
		$stockend = $this->input->get_post('stockend');
		if($stockend)
		{
			$where .= " AND TotalInStock <= $stockend ";
			$this->tpl->assign("stockend",$stockend);
		}
		//序号
		$orderbyxuhao =  $this->input->get('orderbyxuhao');
		if($orderbyxuhao)
		{
			$orderby =" order by id {$orderbyxuhao}";
			if($orderbyxuhao=='ASC')
			{
				$this->tpl->assign("orderbyxuhao",'DESC');
			}else{
				$this->tpl->assign("orderbyxuhao",'ASC');
			}

		}else{
			//$orderby =" order by id ASC";
			$this->tpl->assign("orderbyxuhao",'DESC');
		}
		//商品编号排序
		$orderbysku =  $this->input->get('orderbysku');
		if($orderbysku)
		{
			$orderby =" order by Barcode {$orderbysku}";
			if($orderbysku=='ASC')
			{
				$this->tpl->assign("orderbysku",'DESC');
			}else{
				$this->tpl->assign("orderbysku",'ASC');
			}

		}else{
			$orderby =" order by Barcode ASC";
			$this->tpl->assign("orderbysku",'DESC');
		}
		//商品库存数排序
		$orderbystock =  $this->input->get('orderbystock');
		if($orderbystock)
		{
			$orderby =" order by TotalInStock {$orderbystock}";
			if($orderbystock=='ASC')
			{
				$this->tpl->assign("orderbystock",'DESC');
			}else{
				$this->tpl->assign("orderbystock",'ASC');
			}

		}else{
			//$orderby =" order by Barcode ASC";
			$this->tpl->assign("orderbystock",'DESC');
		}
		$list = $this->fun->getalllist('t_stocks',$where,$nowpage,$if_csv=0,$this->pageSize,$orderby);
		foreach($list['list'] as $key=>$rs)
		{
			//在途数万邑通美西仓
			$rsdb = $this->db->get_one("SELECT A.sku,C.costprice,sum( A.nowbuynum ) AS amount_num,count(*) AS task_num FROM t_transportlist A LEFT JOIN t_transportationtask B ON A.taskid = B.id LEFT JOIN t_overseaproduct C ON A.skuid=C.id WHERE A.taskid>0 AND ( B.status = 2) AND (A.sku like '%".$rs['Barcode']."%' ) AND B.destination ='万邑通美西仓 (USWC)'group by A.`skuid` DESC order by sku ASC");
			$list['list'][$key]['amount_num'] = $rsdb["amount_num"]?$rsdb["amount_num"]:0;
			//复漫系统库存数
			$sql = "SELECT a.stock FROM db_goods_stock a LEFT JOIN db_goods b ON a.goods_id = b.goods_id WHERE b.goods_sku = '{$rs['Barcode']}' AND a.storage_id = 7 ";
			$res = $this->db->query($sql)->row_array();
			$list['list'][$key]['fuman_stock'] = $res["stock"]?$res["stock"]:0;

			//总待发货
			$waitgoods = 0;
			$ress = $this->fun->getdatabasesdetail('db_order_sell a left join db_order_list b on a.order_id = b.order_id '," AND  a.`goods_sn` = '{$rs['Barcode']}' AND b.order_status in (2,3) AND  b.shipping_status = 4 "," sum(a.goods_number) as goods_number");
			if($ress){
				$waitgoods = $waitgoods+$ress['goods_number'];
			}
			$list['list'][$key]['waitgoods'] = $waitgoods; //待发货数量

			//30日内销量  订单列表与fba销量之和
			$salecount = 0;
			$time = time()-30*86400;
			$date = date("Y-m-d H:i:s",$time);
			$result = $this->fun->getdatabasesdetail('db_order_sell a left join db_order_list b on a.order_id = b.order_id '," AND b.dates >= '{$date}' AND b.shipping_status = 4 AND b.order_status in(4,5) AND b.order_source in('正常订单','手工创建','拆分订单','合并订单') AND  a.goods_sn = '{$rs['Barcode']}' "," sum(a.goods_number) as goods_number");
			if($result){
				$salecount = $salecount+$result['goods_number'];
			}
			$list['list'][$key]['salecount'] = $salecount; //30天内的销量
		}
		$pageList = $this->pagenav->page($list["total"], $nowpage, $this->pageSize);
		$this->tpl->assign("totalpage", ceil($list["total"] / $this->pageSize));
		$this->tpl->assign("pageList", $pageList);
		$this->tpl->assign("total", $list["total"]);
		$this->tpl->assign("list",$list["list"]);
		$this ->tpl->display("oversea/winitstocklist.tpl");
	}
	//万邑联 Winit 美国
	public function doCrontabWinitTask(){
		ini_set('max_execution_time',0);
		//判断该请求是否有执行该函数的权限
		$value = $this->input->get('params');
		if($value&&$value=='e29e0068cb5778e7426492247173151d'){
		}else{
			echo json_encode(array('code'=>0,'msg'=>'参数错误!'));exit;
		}
		$date = date("Y-m-d H:i:s");
		//测试环境
		//$url = "http://erp.sandbox.winit.com.cn/ADInterface/api";
		//$username = "rebecca";
		//$password = "888";
		//$token = "89435277FA3BA272DE795559998E-";
		//正式url
		$url = "http://api.winit.com.cn/ADInterface/api";
		$username = $this->config->item('winitusername');
		$password = $this->config->item('winitpass');
		$token = $this->config->item('winittoken');
		//定义数组参数  美西仓 固定 [warehouseID] => 1000008  [warehouseCode] => US0001 [warehouseName] => USWC Warehouse
		$post_data = array();
		$post_data['action'] ="queryWarehouseStorage" ;
		$post_data['app_key'] = $username;
		$post_data['data'] = array("warehouseID"=>"1000008","pageSize"=>50,"pageNum"=>1);
		$post_data['format'] = "json";
		$post_data['language'] ="zh_CN";
		$post_data['platform'] = "SELLERERP";
		$post_data['sign_method'] = "md5";
		$post_data['timestamp'] = $date;
		$post_data['version'] = "1.0";
		//上述为签名所用字段
		$con_data = $token;
		$con_data.= "action".$post_data['action'].
					"app_key".$post_data['app_key'].
					"data".json_encode($post_data['data']).
					"format".$post_data['format'].
					"platform".$post_data['platform'].
					"sign_method".$post_data['sign_method'].
					"timestamp".$post_data['timestamp'].
					"version".$post_data['version'];
		$con_data.= $token;
		$sign = strtoupper(md5($con_data));
		$post_data['sign'] = $sign;
		$json_post = json_encode($post_data);
		//echo $json_post;exit;
		$rs = $this->sendCurlPostRequest($url,$json_post,$headers=array());
		//print_r($rs);exit;
		//返回值
		if($rs['code']==0){
			//获取对应的名称
			$arr_stocks = array();
			foreach($rs['data']['list'] as $k=>$v){
				//$arr_stocks['AuditTime']=$v['AuditTime'];
				$arr_stocks['Barcode']=$v['productCode'];
				$arr_stocks['TotalInStock']=$v['inventory'];
				//执行数据库更新或插入操作 美西仓
				$sql_sel= "SELECT * FROM t_stocks WHERE flag = 3 AND Barcode = '{$v['productCode']}' ";
				$result =$this->db->query($sql_sel)->row_array();
				$addtime = date("Y-m-d H:i:s");
				$arr_stocks['addtime']=$addtime;
				if(count($result)>0){
					$arr_upd['TotalInStock'] =$v['inventory'];
					$arr_upd['addtime'] =$addtime;
					$this->db->where('Barcode',$v['productCode']);
					$this->db->where('flag',3);
					$this->db->update('t_stocks',$arr_upd);
				}else{
					//名字
					$arr_stocks['Name']=$v['productName'];
					$arr_stocks['flag']=3;
					$this->db->insert('t_stocks',$arr_stocks);
				}

			}
			if($rs['data']['total']>50){
				$pageNum = ceil($rs['data']['total']/50);
				for($i=2;$i<=$pageNum;$i++){
					$post_data['data'] = array("warehouseID"=>"1000008","pageSize"=>50,"pageNum"=>$i);
					$con_data = $token;
					$con_data.= "action".$post_data['action'].
						"app_key".$post_data['app_key'].
						"data".json_encode($post_data['data']).
						"format".$post_data['format'].
						"platform".$post_data['platform'].
						"sign_method".$post_data['sign_method'].
						"timestamp".$post_data['timestamp'].
						"version".$post_data['version'];
					$con_data.= $token;
					$sign = strtoupper(md5($con_data));
					$post_data['sign'] = $sign;
					$json_post = json_encode($post_data);
					//echo $json_post;exit;
					$rs = $this->sendCurlPostRequest($url,$json_post,$headers=array());
					//获取对应的名称
					$arr_stocks = array();
					foreach($rs['data']['list'] as $k=>$v){
						//$arr_stocks['AuditTime']=$v['AuditTime'];
						$arr_stocks['Barcode']=$v['productCode'];
						$arr_stocks['TotalInStock']=$v['inventory'];
						//执行数据库更新或插入操作 美西仓
						$sql_sel= "SELECT * FROM t_stocks WHERE flag = 3 AND Barcode = '{$v['productCode']}' ";
						$result =$this->db->query($sql_sel)->row_array();
						$addtime = date("Y-m-d H:i:s");
						$arr_stocks['addtime']=$addtime;
						if(count($result)>0){
							$arr_upd['TotalInStock'] =$v['inventory'];
							$arr_upd['addtime'] =$addtime;
							$this->db->where('Barcode',$v['productCode']);
							$this->db->where('flag',3);
							$this->db->update('t_stocks',$arr_upd);
						}else{
							//名字
							$arr_stocks['Name']=$v['productName'];
							$arr_stocks['flag']=3;
							$this->db->insert('t_stocks',$arr_stocks);
						}

					}

				}
			}

		}

	}
	//模拟post请求
	public function sendCurlPostRequest($url,$post_data,$headers){
		$ch = curl_init($url);
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_POST,1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
		// https请求 不验证证书和hosts
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		$output = curl_exec($ch);
		//var_dump($output);exit;
		curl_close($ch);
		$output= json_decode($output,1);
		return $output;
	}
	//模拟get请求
	public function sendCurlGetRequest($url,$token){
		$headers = array("Authorization: Bearer {$token}");
		$ch = curl_init($url);
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		// https请求 不验证证书和hosts
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		$output = curl_exec($ch);
		curl_close($ch);
		$output= json_decode($output,1);
		return $output;

	}

}
